package cn.tedu.service.order.impl;

import cn.tedu.commons.response.JsonResult;
import cn.tedu.commons.uuid.UUid;


import cn.tedu.mapper.order.OrderMapper;
import cn.tedu.pojo.dto.order.OrderTableDTO;
import cn.tedu.pojo.entity.OrderTeble;


import cn.tedu.pojo.vo.ordervo.OrderStandardVO;
import cn.tedu.service.order.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.time.LocalDateTime;



@Service
@Slf4j

public class OrderAddServiceImpl implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UUid uuid;

    @Override
    public void addOrder(OrderTableDTO orderTebleDTO) {
        //开始插入新增订单
        OrderTeble orderTebleNew = new OrderTeble();
        System.out.println("订单id为：" + orderTebleNew.getOrderId());
        BeanUtils.copyProperties(orderTebleDTO, orderTebleNew);
        System.out.println("来自dto的数据为：" + orderTebleDTO.getOrderAddr());
        orderTebleNew.setCommodityNumber(orderTebleDTO.getCommdityId());
        System.out.println("dto数据：" + orderTebleDTO.getCommdityId());
        //使用雪花ID生成订单编号
        orderTebleNew.setOrderId(uuid.CreateUUIDLong().toString());
        System.out.println(orderTebleNew.getOrderId());
        orderTebleNew.setId(orderTebleDTO.getId());
        orderTebleNew.setOrderPrice(orderTebleDTO.getOrderPrice());
        orderTebleNew.setGmtCreate(LocalDateTime.now());
        orderTebleNew.setOrderCreatetime(LocalDateTime.now());
        orderTebleNew.setGmtModifity(LocalDateTime.now());
        orderMapper.saveOrder(orderTebleNew);


    }

    @Override
    public JsonResult deleteOrder(Long id) {
        int i = orderMapper.saveDeleteOrder(id);
        if (i != 1) {
            throw new RuntimeException("删除失败，订单不存在");
        }
        return JsonResult.ok("删除成功");
    }


    @Override
    public JsonResult updateOrder(OrderStandardVO orderVO) {
        OrderTeble orderTeble = new OrderTeble();
        BeanUtils.copyProperties(orderVO,orderTeble);
        log.info("即将修改的数据为：{}",orderTeble);
        orderTeble.setOrderAddr(orderVO.getOrderAddr());
        orderTeble.setId(orderTeble.getId());
        orderTeble.setCommodityNumber(orderTeble.getCommodityNumber());
        orderMapper.updateOrderById(orderVO.getOrderId(),orderVO.getOrderAddr(),orderVO.getCommodityNumber());
        log.info("修改成功数据为：{}",orderTeble);
        return JsonResult.ok();
    }

    @Override
    public JsonResult selectOrder(String orderId) {
        log.info("id值为："+orderId);
        OrderTableDTO orderTableDTO = orderMapper.selectOrderById(orderId);
        log.info(orderTableDTO.toString());
        if (orderTableDTO == null) {
            throw new RuntimeException("订单不存在,请在此输入");
        }
        return JsonResult.ok(orderTableDTO);
    }


}
